package com.mach.platform.repository;

import com.mach.platform.domain.Order;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface OrderRepo extends BaseRepository<Order> {

    @Query(value = "select * from order_form where order_status > 1 ORDER BY serial_number ASC", nativeQuery = true)
    List<Order> getOrderNotFinish();

    @Query(value = "SELECT MAX(serial_number) as maxSerialNumber FROM `order_form`", nativeQuery = true)
    Integer getOrderSerialNumber();

    @Modifying
    @Transactional
    @Query(value = "update order_form a set " +
            "a.customer = CASE WHEN :#{#order.customer} IS NULL THEN a.customer ELSE :#{#order.customer} END ," +
            "a.number = CASE WHEN :#{#order.number} IS NULL THEN a.number ELSE :#{#order.number} END ," +
            "a.order_id = CASE WHEN :#{#order.orderId} IS NULL THEN a.order_id ELSE :#{#order.orderId} END ," +
            "a.volume =  CASE WHEN :#{#order.volume} IS NULL THEN a.volume ELSE :#{#order.volume} END ," +
            "a.product_category =  CASE WHEN :#{#order.productCategory} IS NULL THEN a.product_category ELSE :#{#order.productCategory} END ," +
            "a.purpose =  CASE WHEN :#{#order.purpose} IS NULL THEN a.purpose ELSE :#{#order.purpose} END ," +
            "a.bodly =  CASE WHEN :#{#order.bodly} IS NULL THEN a.bodly ELSE :#{#order.bodly} END ," +
            "a.bottom_cloth =  CASE WHEN :#{#order.bottomCloth} IS NULL THEN a.bottom_cloth ELSE :#{#order.bottomCloth} END ," +
            "a.thickness =  CASE WHEN :#{#order.thickness} IS NULL THEN a.thickness ELSE :#{#order.thickness} END ," +
            "a.width =  CASE WHEN :#{#order.width} IS NULL THEN a.width ELSE :#{#order.width} END ," +
            "a.printing_wheel_embossing =  CASE WHEN :#{#order.printingWheelEmbossing} IS NULL THEN a.printing_wheel_embossing ELSE :#{#order.printingWheelEmbossing} END ," +
            "a.color_number =  CASE WHEN :#{#order.colorNumber} IS NULL THEN a.color_number ELSE :#{#order.colorNumber} END ," +
            "a.top_coating =  CASE WHEN :#{#order.topCoating} IS NULL THEN a.top_coating ELSE :#{#order.topCoating} END ," +
            "a.base_coating =  CASE WHEN :#{#order.baseCoating} IS NULL THEN a.base_coating ELSE :#{#order.baseCoating} END ," +
            "a.top_coating_color_water =  CASE WHEN :#{#order.topCoatingColorWater} IS NULL THEN a.top_coating_color_water ELSE :#{#order.topCoatingColorWater} END ," +
            "a.backend_process =  CASE WHEN :#{#order.backendProcess} IS NULL THEN a.backend_process ELSE :#{#order.backendProcess} END ," +
            "a.color_number2 =  CASE WHEN :#{#order.colorNumber2} IS NULL THEN a.color_number2 ELSE :#{#order.colorNumber2} END ," +
            "a.dipping =  CASE WHEN :#{#order.dipping} IS NULL THEN a.dipping ELSE :#{#order.dipping} END ," +
            "a.dipping_color_water =  CASE WHEN :#{#order.dippingColorWater} IS NULL THEN a.dipping_color_water ELSE :#{#order.dippingColorWater} END " +
            "where a.id = :#{#order.id}", nativeQuery = true)
    int update(@Param("order") Order order);

    @Modifying
    @Transactional
    @Query(value = "update order_form set operation = ?2 where id = ?1", nativeQuery = true)
    int setOrderOperation(Long id, Integer operation);


    @Modifying
    @Transactional
    @Query(value = "update order_form set order_status = ?2 where id = ?1", nativeQuery = true)
    int setOrderState(Long id, int intState);
}
